<template>
  <div class="login-container">
    <div class="login-header">
      <div class="header-logo">

        <div class="logo-text">
          <h2>大学生自选宿舍管理系统</h2>
        </div>
      </div>
    </div>

    <div class="login-main">
      <div class="login-box">
        <div class="login-header-content">
          <div class="login-avatar">
            <el-icon size="48">
              <UserFilled/>
            </el-icon>
          </div>
          <div class="login-title">
            <h2>欢迎登录</h2>
            <p>大学生自选宿舍管理系统</p>
          </div>
        </div>

        <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="rules"
            class="login-form"
            @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                prefix-icon="User"
                size="large"
                class="login-input"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                size="large"
                show-password
                class="login-input"
                @keyup.enter="handleLogin"
            />
          </el-form-item>

          <el-form-item>
            <el-button
                type="primary"
                size="large"
                :loading="loading"
                class="login-btn"
                @click="handleLogin"
            >
              {{ loading ? '登录中...' : '立即登录' }}
            </el-button>
          </el-form-item>
        </el-form>

      </div>
    </div>

    <!-- 装饰性背景元素 -->
    <div class="bg-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>
  </div>
</template>

<script setup>
import {ref, reactive} from 'vue'
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import {useUserStore} from '../stores/user'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, message: '密码长度不能少于6位', trigger: 'blur'}
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const response = await userStore.login(loginForm)
        ElMessage.success('登录成功')

        // 根据用户角色跳转
        if (userStore.userInfo.role === 'admin') {
          router.push('/admin/dashboard')
        } else {
          router.push('/student/dashboard')
        }
      } catch (error) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: #e4e4e4;
  position: relative;
  overflow: hidden;
}

.login-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 64px;
  background: #409EFF;
  display: flex;
  align-items: center;
  padding: 0 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.header-logo {
  display: flex;
  align-items: center;
  color: white;
}

.logo-icon {
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: #409EFF;
  border-radius: 6px;
  color: white;
}

.logo-text h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: white;
}

.login-main {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 80px 20px 20px;
  position: relative;
  z-index: 5;
}

.login-box {
  background: white;
  padding: 48px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 420px;
  position: relative;
}

.login-box::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: #409EFF;
  border-radius: 12px 12px 0 0;
}

.login-header-content {
  text-align: center;
  margin-bottom: 40px;
}

.login-avatar {
  width: 80px;
  height: 80px;
  background: #f5f7fa;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 24px;
  color: #409EFF;
  border: 3px solid #e4e7ed;
  transition: all 0.3s ease;
}

.login-avatar:hover {
  border-color: #409EFF;
  background: rgba(64, 158, 255, 0.1);
}

.login-title h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
}

.login-title p {
  margin: 0;
  font-size: 14px;
  color: #909399;
  font-weight: 400;
}

.login-form {
  margin-bottom: 32px;
}

.login-form .el-form-item {
  margin-bottom: 24px;
}

.login-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: 0 0 0 1px #e4e7ed inset;
  transition: all 0.2s ease;
}

.login-input :deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #409EFF inset;
}

.login-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409EFF inset;
}

.login-input :deep(.el-input__inner) {
  font-size: 14px;
  color: #303133;
}

.login-input :deep(.el-input__prefix) {
  color: #909399;
}

.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  background: #409EFF;
  border: none;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.2s ease;
}

.login-btn:hover {
  background: #337ecc;
  transform: translateY(-1px);
}

.login-btn:active {
  transform: translateY(0);
}

.login-tips {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  border: 1px solid #e4e7ed;
}

.tips-title {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  font-size: 14px;
  font-weight: 600;
  color: #606266;
}

.tips-title .el-icon {
  margin-right: 8px;
  color: #409EFF;
}

.tips-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.tip-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: white;
  border-radius: 6px;
  border: 1px solid #e4e7ed;
  transition: all 0.2s ease;
}

.tip-item:hover {
  border-color: #409EFF;
  background: rgba(64, 158, 255, 0.05);
}

.tip-label {
  font-size: 12px;
  color: #909399;
  min-width: 80px;
}

.tip-value {
  font-size: 12px;
  color: #303133;
  font-weight: 500;
  font-family: 'Courier New', monospace;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  overflow: hidden;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.05);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  right: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  bottom: 20%;
  left: 15%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  top: 60%;
  right: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-main {
    padding: 80px 16px 16px;
  }

  .login-box {
    padding: 32px 24px;
  }

  .login-title h2 {
    font-size: 20px;
  }

  .tips-content {
    gap: 8px;
  }

  .tip-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .tip-label {
    min-width: auto;
  }
}

@media (max-width: 480px) {
  .header-logo .logo-text {
    display: none;
  }

  .login-box {
    padding: 24px 20px;
  }

  .login-avatar {
    width: 64px;
    height: 64px;
  }

  .login-title h2 {
    font-size: 18px;
  }
}
</style>
